.TH std::ranges::views::chunk_by,std::ranges::chunk_by_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::views::chunk_by,std::ranges::chunk_by_view \- std::ranges::views::chunk_by,std::ranges::chunk_by_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::forward_range V,
   std::indirect_binary_predicate<iterator_t<V>,

             ranges::iterator_t<V>> Pred >                            \fB(1)\fP (since C++23)
       requires ranges::view<V> && std::is_object_v<Pred>
   class chunk_by_view

       : public ranges::view_interface<chunk_by_view<V, Pred>>
   namespace views {

       inline constexpr /* unspecified */ chunk_by = /* unspecified   \fB(2)\fP (since C++23)
   */ ;

   }
   Call signature
   template< ranges::viewable_range R, class Pred >

       requires /* see below */                                           (since C++23)

   constexpr ranges::view auto chunk_by( R&& r, Pred&& pred );
   template< class Pred >                                                 (since C++23)
   constexpr /*range adaptor closure*/ chunk_by( Pred&& pred );

   1) chunk_by_view is a range adaptor that takes a view and an invocable object pred
   (the binary predicate), and produces a view of subranges (chunks), by splitting the
   underlying view between each pair of adjacent elements for which pred returns false.
   The first element of each such pair belongs to the previous chunk, and the second
   element belongs to the next chunk.
   2) The name views::chunk_by denotes a RangeAdaptorObject. Given a subexpression e
   and f, the expression views::chunk_by(e, f) is expression-equivalent to
   chunk_by_view(e, f).

   chunk_by_view always models forward_range, and models bidirectional_range and/or
   common_range, if adapted view type models the corresponding concepts. chunk_by_view
   never models borrowed_range or sized_range.

.SH Member functions

   constructor   constructs a chunk_by_view
   (C++23)       \fI(public member function)\fP
   base          returns a copy of the underlying (adapted) view
   (C++23)       \fI(public member function)\fP
   pred          returns a reference to the stored predicate
   (C++23)       \fI(public member function)\fP
   begin         returns an iterator to the beginning
   (C++23)       \fI(public member function)\fP
   end           returns an iterator or a sentinel to the end
   (C++23)       \fI(public member function)\fP
   find_next     returns an iterator to the begin of the next subrange
   (C++23)       (exposition-only member function*)
   find_prev     returns an iterator to the begin of the previous subrange
   (C++23)       (exposition-only member function*)
         Inherited from std::ranges::view_interface
   empty         returns whether the derived view is empty. Provided if it satisfies
   (C++20)       sized_range or forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   back          returns the last element in the derived view. Provided if it satisfies
   (C++20)       bidirectional_range and common_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

   Deduction guides

   Nested classes

   iterator the iterator type
   (C++23)  (exposition-only member class template*)

.SH Notes

   In order to provide the amortized constant time complexity required by the range
   concept, the result of begin() is cached within the chunk_by_view object. If the
   underlying range is modified after the first call to begin(), subsequent uses of the
   chunk_by_view object might have unintuitive behavior.

      Feature-test macro      Value    Std            Feature
   __cpp_lib_ranges_chunk_by 202202L (C++23) std::ranges::chunk_by_view

.SH Example


// Run this code

 #include <functional>
 #include <iostream>
 #include <ranges>
 #include <string_view>
 #include <vector>

 void print_chunks(auto view, std::string_view separator = ", ")
 {
     for (auto const subrange : view)
     {
         std::cout << '[';
         for (std::string_view prefix; auto const& elem : subrange)
             std::cout << prefix << elem, prefix = separator;
         std::cout << "] ";
     }
     std::cout << '\\n';
 }

 int main()
 {
     {
         auto v = std::vector{1, 2, 3, 1, 2, 3, 3, 3, 1, 2, 3};
         auto fun = std::ranges::less{};
         auto view = v | std::views::chunk_by(fun);
         print_chunks(view);
     }
     {
         auto v = std::vector{1, 2, 3, 4, 4, 0, 2, 3, 3, 3, 2, 1};
         auto fun = std::not_fn(std::ranges::equal_to{}); // or ranges::not_equal_to
         auto view = v | std::views::chunk_by(fun);
         print_chunks(view);
     }
     {
         std::string v = "__cpp_lib_ranges_chunk_by";
         auto fun = [](char x, char y) { return not(x == '_' or y == '_'); };
         auto view = v | std::views::chunk_by(fun);
         print_chunks(view, "");
     }
 }

.SH Output:

 [1, 2, 3] [1, 2, 3] [3] [3] [1, 2, 3]
 [1, 2, 3, 4] [4, 0, 2, 3] [3] [3, 2, 1]
 [_] [_] [cpp] [_] [lib] [_] [ranges] [_] [chunk] [_] [by]

.SH References

     * C++23 standard (ISO/IEC 14882:2023):

     * 26.7.30 Chunk by view [range.chunk.by]

.SH See also

   ranges::chunk_view  a range of views that are N-sized non-overlapping successive
   views::chunk        chunks of the elements of another view
   (C++23)             \fI(class template)\fP (range adaptor object)
   ranges::slide_view  a view whose M^th element is a view over the M^th through (M + N
   views::slide        - 1)^th elements of another view
   (C++23)             \fI(class template)\fP (range adaptor object)
   ranges::stride_view a view consisting of elements of another view, advancing over N
   views::stride       elements at a time
   (C++23)             \fI(class template)\fP (range adaptor object)
